package com.zhangxiaofanfan.hotcode.second;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <a href="https://leetcode.cn/problems/permutations/description/?envType=study-plan-v2&envId=top-100-liked">46. 全排列</a>
 *
 * @author zhangxiaofanfan
 * @date 2024-06-26 20:33:33
 */
public class HotCode055 {
    public static void main(String[] args) {

    }

    private List<List<Integer>> result = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        if (nums == null || nums.length == 0) {
            return result;
        }
        bfs(nums, new ArrayList<>(), new HashSet<>());
        return result;
    }

    public void bfs(int[] nums, List<Integer> list, Set<Integer> set) {
        if (set.size() == nums.length) {
            result.add(new ArrayList<>(list));
        }
        for (int i = 0; i < nums.length; i++) {
            if (set.contains(i)) {
                continue;
            }
            list.add(nums[i]);
            set.add(i);
            bfs(nums, list, set);
            list.removeLast();
            set.remove(i);
        }
    }

}
